// 删除
$('#delBtn').click(
    function(e) {
        e.preventDefault();
        var groupId = $("#groupId").val();
        if (groupId == '' || groupId == undefined || groupId == null) {
            bootbox.alert("请选择一个删除的节点");
        } else {
            bootbox.confirm(
                "您确定删除该部门?",
                function(result) {
                    if (result) {
                        $.ajax({
                            type: "post",
                            url: ctx + "/employeeGroup/delete",
                            data: "groupId=" + groupId,
                            dataType: "json",
                            success: function(msg) {
                                if (msg.code == 1) {
                                    bootbox.dialog({
                                        message: "删除成功！",
                                        title: "温馨提示：",
                                        buttons: {
                                            main: {
                                                label: "确定",
                                                className: "btn btn-primary",
                                                callback: function() {
                                                    window.location.reload();
                                                }
                                            }
                                        }
                                    });
                                } else {
                                    bootbox.dialog({
                                        message: msg.message,
                                        title: "温馨提示：",
                                        buttons: {
                                            main: {
                                                label: "确定",
                                                className: "btn btn-primary",
                                                callback: function() {
                                                    window.location.reload();
                                                }
                                            }
                                        }
                                    });
                                }
                            },
                            error: function(msg) {
                                bootbox.dialog({
                                    message: "删除失败！",
                                    title: "温馨提示：",
                                    buttons: {
                                        main: {
                                            label: "确定",
                                            className: "btn btn-primary",
                                            callback: function() {
                                                window.location.reload();
                                            }
                                        }
                                    }
                                });
                            }
                        });
                    }
                });
        }
    });
// 添加
$('#addBtn').click(
    function(e) {
        e.preventDefault();
        var groupId = $("#groupId").val();
        if (groupId == '' || groupId == undefined || groupId == null) {
            bootbox.alert("请选择一个父节点");
        } else {
            e.preventDefault();
            $.ajax({
                type: "post",
                url: ctx + "/employeeGroup/checkGroupId",
                data: "groupId=" + groupId,
                dataType: "json",
                success: function(msg) {
                    if (msg.code == 1) {
                        $("#addEmpGroupModelBody").empty();
                        var tmpl = $("#addEmpGroupModelTemplate").tmpl("").appendTo("#addEmpGroupModelBody");
                        var modal = $('#addEmpGroupModel').modal('show');
                        $('#addEmpGroupBtn').unbind("click").click(function(e) {
                            e.preventDefault();
                            modal.hide();
                            var groupName = $("#groupNameAdd").val();
                            var plannerGroup = 0;
                            if ($("#plannerGroupAdd").is(':checked')) {
                                plannerGroup = 1;
                            };
                            if (groupNameAdd == "") {
                                bootbox.alert("请输入团队名称！");
                                return;
                            };
                            $.ajax({
                                type: "post",
                                url: ctx + "/employeeGroup/doAdd",
                                data: {
                                    "groupName": groupName,
                                    "plannerGroup": plannerGroup,
                                    "parent": groupId,
                                },
                                dataType: "json",
                                success: function(msg) {
                                    if (msg.code == 1) {
                                        bootbox.dialog({
                                            message: "新增部门成功！",
                                            title: "温馨提示：",
                                            buttons: {
                                                main: {
                                                    label: "确定",
                                                    className: "btn btn-primary",
                                                    callback: function() {
                                                        window.location.reload();
                                                    }
                                                }
                                            }
                                        });
                                    } else {
                                        bootbox.dialog({
                                            message: msg.message,
                                            title: "温馨提示：",
                                            buttons: {
                                                main: {
                                                    label: "确定",
                                                    className: "btn btn-primary",
                                                    callback: function() {
                                                        window.location.reload();
                                                    }
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                        });
                    } else {
                        bootbox.dialog({
                            message: msg.message,
                            title: "温馨提示：",
                            buttons: {
                                main: {
                                    label: "确定",
                                    className: "btn btn-primary",
                                    callback: function() {
                                        window.location.reload();
                                    }
                                }
                            }
                        });
                    };
                },
                error: function(msg) {
                    bootbox.dialog({
                        message: "服务器出问题了！",
                        title: "温馨提示：",
                        buttons: {
                            main: {
                                label: "确定",
                                className: "btn btn-primary",
                                callback: function() {
                                    window.location.reload();
                                }
                            }
                        }
                    });
                }
            });
        }
    });
// 编辑
$('#editBtn').click(function(e) {
    e.preventDefault();
    var id = $("#groupId").val();
    if (id == '' || id == undefined || id == null || id == "root") {
        bootbox.alert("请选择一个节点");
        return;
    }
    e.preventDefault();
    $.ajax({
        type: "post",
        url: ctx + "/employeeGroup/get",
        data: "id=" + id,
        dataType: "json",
        success: function(msg) {
            if (msg.code == 1) {
                $("#editEmpGroupModelBody").empty();
                var tmpl = $("#editEmpGroupModelTemplate").tmpl(msg.data).appendTo("#editEmpGroupModelBody");
                var modal = $('#editEmpGroupModel').modal('show');
                
                $("#plannerGroupUpdata").unbind("click").click(function(e){
                	if (msg.data.plannerGroup == true && plannerGroupUpdata.checked==false) {
                        bootbox.dialog({
                            message: "修改为非理财师团队后，数据统计等功能会受到影响，请谨慎选择。",
                            title: "温馨提示：",
                            buttons: {
                                main: {
                                    label: "确定",
                                    className: "btn btn-primary",
                                    callback: function() {
                                        
                                    }
                                }
                            }
                        });
                    };
                });
                
                $("#editEmpGroupBtn").unbind("click").click(function(e) {
                    e.preventDefault();
                    var groupName = $("#groupNameUpdata").val();
                    if (groupName == null || groupName == '') {
                        bootbox.alert("请输入部门名称！");
                        return;
                    }
                    var plannerGroup = 0;
                    if ($("#plannerGroupUpdata").is(':checked')) {
                        plannerGroup = 1;
                    };
                    $.ajax({
                        type: "post",
                        url: ctx + "/employeeGroup/update",
                        data: {
                            "groupId": id,
                            "groupName": groupName,
                            "plannerGroup": plannerGroup
                        },
                        dataType: "json",
                        success: function(updateMsg) {
                            if (updateMsg.code == 1) {
                                bootbox.dialog({
                                    message: "更新部门信息成功！",
                                    title: "温馨提示：",
                                    buttons: {
                                        main: {
                                            label: "确定",
                                            className: "btn btn-primary",
                                            callback: function() {
                                                window.location.reload();
                                            }
                                        }
                                    }
                                });
                            } else {
                                bootbox.dialog({
                                    message: updateMsg.message,
                                    title: "温馨提示：",
                                    buttons: {
                                        main: {
                                            label: "确定",
                                            className: "btn btn-primary",
                                            callback: function() {

                                            }
                                        }
                                    }
                                });
                            }

                        },
                        error: function(updateMsg) {
                            bootbox.dialog({
                                message: "更新部门信息失败！",
                                title: "温馨提示：",
                                buttons: {
                                    main: {
                                        label: "确定",
                                        className: "btn btn-primary",
                                        callback: function() {

                                        }
                                    }
                                }
                            });
                        }
                    });
                });

            } else {
                bootbox.dialog({
                    message: msg.message,
                    title: "温馨提示：",
                    buttons: {
                        main: {
                            label: "确定",
                            className: "btn btn-primary",
                            callback: function() {
                                window.location.reload();
                            }
                        }
                    }
                });
            };
        },
        error: function(msg) {
            bootbox.dialog({
                message: "更新部门信息失败！",
                title: "温馨提示：",
                buttons: {
                    main: {
                        label: "确定",
                        className: "btn btn-primary",
                        callback: function() {

                        }
                    }
                }
            });
        }
    });


});